package edu.njust.cma.controller;

import edu.njust.cma.pojo.Result;
import edu.njust.cma.pojo.Standard;
import edu.njust.cma.service.StandardService;
import edu.njust.cma.util.AliOSSUtils;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/standard")
public class StandardController {

    @Autowired
    AliOSSUtils aliOSSUtils;
    @Autowired
    StandardService standardService;
    /**
     * 新建标准
     * 1 .先上传文件，再通过上传文件的URL生成标准
     */
    @PostMapping("/addStandard/file")
    public Result addStandardFile(@RequestBody MultipartFile file) throws IOException {
        String url = aliOSSUtils.upload(file);
        Result result = Result.success(url);
        return result;
    }
    @PostMapping("/addStandard")
    public Result addStandard(@RequestBody Standard standard){
        standardService.addStandard(standard);
        return Result.success();
    }

    // 为某个标准添加参数
    @GetMapping("/addStandard/{standardId}/{methodName}")
    public Result addStandardMethod(@PathVariable int standardId, @PathVariable String methodName){
        standardService.addStandardMethod(standardId,methodName);
        return Result.success();
    }

    /**
     * 查看所有的标准
     * */
    @GetMapping("/getAll")
    public Result getAllStandard(){
        return Result.success(standardService.getAllStandard());
    }
    /**
     * 查看所有的大类名字
     * */
    @GetMapping("/getAllMajorCategoryName")
    public Result getAllMajorCategoryName(){
        return Result.success(standardService.getAllMajorCategoryName());
    }
    /**
    * 获取某个大类的所有小类名字
     */
    @GetMapping("/getAllMinorCategoryName/{MajorCategoryName}")
    public Result getAllMinorCategoryName(@PathVariable String MajorCategoryName){
        return Result.success(standardService.getAllMinorCategoryName(MajorCategoryName));
    }
    /**
     * 获取某个大类,小类名字的编号
     */
    @GetMapping("/getNumber/{MajorCategoryName}/{MinorCategoryName}")
    public Result getNumber(@PathVariable String MajorCategoryName,@PathVariable String MinorCategoryName){
        return Result.success(standardService.getNumber(MajorCategoryName,MinorCategoryName));
    }
    /**
     * 获取某个编号的标识符
     */
    @GetMapping("/getStandard/{Number}")
    public Result getStandard(@PathVariable String Number){
        return Result.success(standardService.getStandardId(Number));
    }
    /**
     * 获取某个标准的所有参数
     */
    @GetMapping("/getStandardMethod/{StandardId}")
    public Result getStandardMethod(@PathVariable int StandardId){
        return Result.success(standardService.getStandardMethod(StandardId));
    }

    /**
     * 删除某个标准
     */
    @DeleteMapping("/deleteStandard/{StandardId}")
    public Result deleteStandard(@PathVariable int StandardId){
        if(standardService.deleteStandard(StandardId) ){
            return Result.success();
        }else {
            return Result.error("删除失败,有未完成的项目正在参考这个标准");
        }
    }

    /**
     * 删除某个标准的某个参数
     */
    @Delete("/deleteStandardMethod/{StandardId}/{MethodName}")
    public Result deleteStandardMethod(@PathVariable int StandardId,@PathVariable String MethodName){
//        return Result.success(standardService.deleteStandardMethod(StandardId,MethodName));
        return Result.success();
    }

}
